package com.ba.base.view

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Build
import android.util.Log
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.ba.base.R
import com.ba.base.utils.LogUtils
import com.ba.base.utils.ThemeHelper
import com.ba.base.view.web.WebActivity
import com.tencent.smtt.sdk.WebView
import kotlinx.android.synthetic.main.app_toolbar_back.*
import qsos.lib.base.callback.OnTListener
import com.tencent.smtt.sdk.WebViewClient

@Route(group = "normal", path = "/normal/web")
class NormalWebActivity(
    override val layoutId: Int = R.layout.normal_activity_web,
    override val reload: Boolean = false
) : WebActivity() {

    @Autowired(name = "url")
    @JvmField
    var mUrl: String? = null

    @Autowired(name = "title")
    @JvmField
    var mTitleName: String? = null

    @Autowired(name = "rightTitle")

    @JvmField
    var rightTitle: String? = null

    @Autowired(name = "titleUrl")
    @JvmField
    var titleUrl: String? = null
    var startTime: Double = 0.0
    var endTime: Double = 0.0
    override fun initView() {
        super.initView()
//        LogUtils.d("rightTitle", rightTitle)
//        LogUtils.d("titleUrl", titleUrl)
        setTitleName(mTitleName ?: "")
        rightTitle?.let {
            setRight(rightTitle!!)
            app_title_right?.setOnClickListener {
                //帮助
                ARouter.getInstance().build("/normal/common/web")
                    .withString("url", titleUrl)
                    .withString("title", "帮助").navigation()
            }
        }
        LogUtils.d("NormalWebActivity ${mUrl} ${mTitleName}", "")
        mWebView?.loadUrl(mUrl)
        mWebView?.webViewClient = object : WebViewClient() {
            override fun onPageFinished(p0: WebView?, p1: String?) {
                super.onPageFinished(p0, p1)
                endTime = System.currentTimeMillis().toDouble() - startTime
                Log.d("TAG", "onPageFinished: " + endTime)
            }

            override fun onPageStarted(p0: WebView?, p1: String?, p2: Bitmap?) {
                super.onPageStarted(p0, p1, p2)
                startTime = System.currentTimeMillis().toDouble()
            }
        }
        setStatusBarTransparent()
    }

    override fun getData() {}
    override fun notifyTheme(data: ThemeData) {
    }

    @SuppressLint("ResourceAsColor")
    override fun onResume() {
        super.onResume()
        ThemeHelper.setColor(
            mContext as Activity, Color.parseColor("#00000000"), 0
        )
        ThemeHelper.setLightMode(mContext as Activity)
    }

    /**
     * 把状态栏设成透明
     */
    private fun setStatusBarTransparent() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val decorView = window.decorView
            decorView.setOnApplyWindowInsetsListener { v, insets ->
                val defaultInsets = v.onApplyWindowInsets(insets)
                defaultInsets.replaceSystemWindowInsets(
                    defaultInsets.systemWindowInsetLeft,
                    0,
                    defaultInsets.systemWindowInsetRight,
                    defaultInsets.systemWindowInsetBottom
                )
            }
            ViewCompat.requestApplyInsets(decorView)
            window.statusBarColor = ContextCompat.getColor(this, android.R.color.transparent)
        }
    }
}